package com.nubia.controller;


import com.nubia.entity.Shops;
import com.nubia.entity.User;
import com.nubia.service.AdminService;
import com.nubia.service.CartService;
import com.nubia.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("admin")
@Slf4j
public class AdminController {
    @Autowired
    AdminService adminService;


    /**
     * 查看用户列表
     *
     */
    @RequestMapping("/adminFindAllUser")
     public Map<String,Object> AdminFindUser() {
          List<User> adminUserList = adminService.adminFindAllUser();
          if(adminUserList !=null){
               return Result.resultData("获取成功", true, adminUserList);

          } else {
               return Result.resultData("获取失败", true, null);
          }
    }

    /**
     * 管理员对用户进行删除
     *
     * @param user
     * @return
     */
    @RequestMapping("delUser")
    public Map<String, Object> deleteUser(@RequestBody User user) {
        if (user.getUser_id() != null) {
            try {
                int i = adminService.delUserById(user);
                return Result.resultDataString("删除个人商品成功", true, "success");

            } catch (Exception e) {
                e.printStackTrace();
                return Result.resultDataString("个人购物车获取成功", false, "系统异常，联系后台");
            }

        } else {

            return Result.resultDataString("删除个人商品失败", false, "失败");
        }

    }

    /**
     * 管理员查看商品列表
     */
    @RequestMapping("/AdminGetShopsList")
    public Map<String, Object> AdminGetAllShops() {
        List<Shops> adminshopsList = adminService.adminFindAllShops();
        if (adminshopsList != null) {
            return Result.resultData("获取成功", true, adminshopsList);
        } else {
            return Result.resultData("获取失败", false, null);
        }
    }

    /**
     * 添加一条商品
     */
    @RequestMapping("/adminAddShop")
    public Map<String, Object> AdminIntoInsShop(@RequestBody Shops shops) {
        //对shop_id设置随机                                                                               
        shops.setShop_id(UUID.randomUUID().toString());
        if (shops.getShop_name() != null) {
            int i = adminService.adminInsShop(shops);
            if (i == 0) {
                return Result.resultDataString("加入商品失败", false, "失败");
            } else {
                return Result.resultDataString("加入商品成功", true, "success");
            }

        } else {
            return Result.resultDataString("已经存在该商品", false, "请换其他商品");
        }


    }

    /**
     * 删除商品
     */
    @RequestMapping("/adminDelShop")
    public Map<String, Object> delUserOneCart(@RequestBody Shops shops) {
        if (shops.getShop_id() != null) {
            try {
                int i = adminService.adminDleShop(shops.getShop_id());
                return Result.resultDataString("删除个人商品成功", true, "success");

            } catch (Exception e) {
                e.printStackTrace();
                return Result.resultDataString("失败", false, "系统异常，联系后台");
            }

        } else {

            return Result.resultDataString("删除个人商品失败", false, "失败");
        }
    }


    /**
     * 修改商品
     */
    @RequestMapping("adminUpdShop")
    public Map<String, Object> adminNewShops(@RequestBody Shops shops) {

        if (shops.getShop_id() != null) {
            try {
                int UpdShop = adminService.adminUpdShop(shops);
                return Result.resultData("添加成功", true, UpdShop);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.resultDataString("失败", false, "系统异常，联系后台");
            }
        } else {
            return Result.resultDataString("不存在改用户", false, "系统异常，联系后台");
        }


    }
}
